﻿using System.Linq;
using System.Text;

namespace MyLib {
	/// <summary>
	/// 
	/// </summary>
	public static class NameHelper {
		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static string ToPascal(this string name) {
			System.Diagnostics.Debug.Assert(!string.IsNullOrWhiteSpace(name));

			var cc = new char[name.Length];
			var flag1 = true; // First letter of group.
			var flag2 = true; // First letter of word.
			var k = 0;
			for (int i = 0; i < cc.Length; ++i) {
				var c = name[i];

				if (flag1) {
					if (char.IsLetter(c)) {
						c = char.ToUpper(c);
						flag1 = false;
						flag2 = false;
					} else {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					}
				} else if (flag2) {
					if (char.IsLetter(c)) {
						c = char.ToUpper(c);
						flag1 = false;
						flag2 = false;
					} else {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					}
				} else {
					if (c == '.') {
						flag1 = true;
						flag2 = true;
					} else if (c == '_') {
						flag1 = false;
						flag2 = true;
						continue;
					} else if (!char.IsLetterOrDigit(c)) {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					} else {
						c = char.ToLower(c);
					}
				}

				cc[k++] = c;
			}

			return new string(cc, 0, k);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static string ToCamel(this string name) {
			System.Diagnostics.Debug.Assert(!string.IsNullOrWhiteSpace(name));

			var cc = new char[name.Length];
			var flag1 = true; // First letter of group.
			var flag2 = true; // First letter of word.
			var k = 0;
			for (int i = 0; i < cc.Length; ++i) {
				var c = name[i];

				if (flag1) {
					if (char.IsLetter(c)) {
						c = char.ToLower(c);
						flag1 = false;
						flag2 = false;
					} else {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					}
				} else if (flag2) {
					if (char.IsLetter(c)) {
						c = char.ToUpper(c);
						flag1 = false;
						flag2 = false;
					} else {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					}
				} else {
					if (c == '.') {
						flag1 = true;
						flag2 = true;
					} else if (c == '_') {
						flag1 = false;
						flag2 = true;
						continue;
					} else if (!char.IsLetterOrDigit(c)) {
						throw new System.FormatException(
							string.Format("Invalid name: \"{0}\", character at {1}.", name, i));
					} else {
						c = char.ToLower(c);
					}
				}

				cc[k++] = c;
			}

			return new string(cc, 0, k);
		}
	} // end of NameHelper.
}
